package com.linchong.concurrency.chapter4;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author:linchong
 * @CreateTime:2020-01-03 13:48
 * @Description:守护线程
 */
@Slf4j
public class DaemonThread {

	public static void main(String[] args) throws InterruptedException {
		//new
		Thread t = new Thread() {
			@Override
			public void run() {
				try {
					log.info("currentThreadName: {} ", Thread.currentThread().getName() + " running");
					Thread.sleep(100000);
					log.info("currentThreadName: {} ", Thread.currentThread().getName() + " running");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		//runnable -> running -> dead -> blocked

		//设置守护进程，main结束即结束，应该在线程启动前调用，如果在启动后调用，抛出IllegalThreadStateException
		t.setDaemon(true);
		t.start();

		Thread.sleep(5_000);
		log.info("currentThreadName: {} ", Thread.currentThread().getName() + " running");
	}
}
